package com.four.project.controller;

import com.four.project.pojo.CustomeReq;
import com.four.project.service.CustomeService;
import com.four.project.pojo.CustomeResp;
import com.four.project.pojo.Customer;
import com.four.project.utils.MD5Util;
import com.four.project.utils.UploadUtils;
import com.sun.org.apache.bcel.internal.generic.I2D;
import org.apache.catalina.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.Id;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by 54110 on 2020/7/1.
 */
@RestController
@CrossOrigin
public class CustomController {

    @Autowired
    CustomeService customeService;

    @RequiresPermissions(value = {"add"})
    @RequestMapping("/addcustom")
    public String insert(@RequestBody Customer customer){
        return customeService.insert(customer);
    }


    @RequiresPermissions(value = {"query"})
    @RequestMapping("/findAll/{page}/{size}")
    public CustomeResp findAll(@PathVariable("page")Integer page, @PathVariable("size")Integer size){
            return customeService.findAll(page,size);
    }
    @RequiresPermissions(value = {"update"})
    @PostMapping("/updateCustom")
    public String update(@RequestBody Customer customer){
        Customer resp = customeService.updateCustom(customer);
        if (resp!=null){
            return "success";
        }else{
            return "error";
        }
    }
    @RequiresPermissions(value = {"delete"})
    @DeleteMapping("/deleteCustombyid/{id}")
    public void deleteCustom(@PathVariable("id") Integer id){
        customeService.deleteById(id);
    }



    /**
     * 发送验证码
     * @param email
     * @param request
     * @return
     */
    @RequestMapping(value = "/sendMail",method = RequestMethod.POST)
    @ResponseBody
    public  String sendMail(@RequestParam String email, HttpServletRequest request){
        try {
            customeService.sendMail(email,request);
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }

    /**
     * 用户注册方法
     * @param customeReq
     * @param request
     * @return
     */
    @RequestMapping(value = "/registory",method = RequestMethod.POST)
    @ResponseBody
    public String registory(CustomeReq customeReq, HttpServletRequest request){
        String code = customeReq.getCode();//用户输入的验证码
        String attribute =(String) request.getAttribute(customeReq.getEmail());//服务器存储的验证码
        if (code.equals(attribute)){
            return "fail";//验证码错误
        }
        Customer customer = new Customer();
        customer.setEmail(customeReq.getEmail());
        customer.setName(customeReq.getUsername());
        //将密码使用MD5加密
//        String MD5password = MD5Util.toMD5(customeReq.getPassword());
//        customer.setPassword(MD5password);
        customer.setPassword(customeReq.getPassword());
        return customeService.insert(customer);
    }

    /**
     * 用户登录验证
     * @param customeReq
     * @param request
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(CustomeReq customeReq,HttpServletRequest request){
        return  customeService.login(customeReq,request);
    }

}
